<?php

namespace App\Service\Index\v1_3_0;

use App\Constants\ErrorCode;
use App\Model\Act\HostTaskReward;
use App\Model\CoinLog;
use App\Model\Service\ConsumeService;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 主播任务领取
 */
class HostTaskRewardService extends BaseService
{
    public function handle()
    {
	return $this->setError(ErrorCode::PARAMETER_ERROR, '任务已关闭');
	$userId = $this->getUserId();
        $taskType = $this->request->input('task_type', '');
        //是否女的
        $userProfile = UserProfile::getProfile($userId);
        if ($userProfile->gender != 0) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '当前身份暂时无法领取');
        }
        $hostTaskConf = config('act.host_task');
        if (!isset($hostTaskConf[$taskType])) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '任务不存在，暂无法操作！');
        }
        $coin = $hostTaskConf[$taskType]['coin'] ?? '0';
        //领取过的
        $check = HostTaskReward::checkDone($userId, $taskType);
        if ($check > 0) return $this->setError(ErrorCode::PARAMETER_ERROR, '你已经领取过了～');
        //已领取状态
        $result = HostTaskReward::updateDone($userId, $taskType, $coin);
        if ($result) {
            $result = ConsumeService::gainIncome($userId, $coin, CoinLog::FROM_NEW_GUIDE_TASK);
        }
        // log_user_result($userId, $ret);
        return ['result' => $result ? 1 : 0, 'tip' => "{$coin}金币已被领取"];
    }
}
